package com.ysstech.valuation.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ysstech.common.cache.RedisCacheManager;
import com.ysstech.common.util.DateUtil;
import com.ysstech.valuation.entity.Quotation;
import com.ysstech.valuation.mapper.QuotationMapper;
import com.ysstech.valuation.service.QuotationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-02-22
 */
@Service
public class QuotationServiceImpl extends ServiceImpl<QuotationMapper, Quotation> implements QuotationService {
    @Autowired
    private RedisCacheManager redisCacheManager;
    @Autowired
    private QuotationMapper quotationMapper;


    /**
     * 获取股票类行情 包含  除权除息报价
     *
     * @param securityCode
     * @param date
     * @return
     * @throws Exception
     */
    public Quotation getQuotation(String securityCode, LocalDate date) throws Exception {
        Map<String, List<Quotation>> mapQuotation = this.queryQuotationCache(date);
        List<Quotation> list = mapQuotation.get(date + "\t" + securityCode);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
        // 除权除息报价=（股权登记日收盘价+配股价×配股比例-每股现金红利）/（1+每股送股比例+每股配股比例）。
        //重新计算行情价格
    }

    /**
     * 获取行情信息
     *
     * @return
     * @throws Exception
     */
    public Map<String, List<Quotation>> queryQuotationCache(LocalDate fdate) throws Exception {
        try {
            if (null == fdate) {
                return null;
            }
            if (redisCacheManager.exists("QUOTATION_" + DateUtil.localDateToStr(fdate))) {
                return (Map<String, List<Quotation>>) redisCacheManager.getCache("QUOTATION_" + DateUtil.localDateToStr(fdate));
            }
            //查询所有
            Quotation Quotation = new Quotation();
            Quotation.setFdate(fdate);
            List<Quotation> Quotations = quotationMapper.queryQuotationLate(Quotation);
            if (null == Quotations || Quotations.size() == 0) {
                return null;
            }
            Map<String, List<Quotation>> mapQuotation = new HashMap<>();
            for (Quotation quotation : Quotations) {
                String key = fdate + "\t" + quotation.getSecurityCode();
                List<Quotation> list = new ArrayList<>();
                if (mapQuotation.containsKey(key)) {
                    list = mapQuotation.get(key);
                    if (!CollectionUtils.isEmpty(list)) {
                        list.add(quotation);
                        mapQuotation.put(key, list);
                    }
                } else {
                    list.add(quotation);
                    mapQuotation.put(key, list);
                }
            }
            redisCacheManager.putCache("QUOTATION_" + DateUtil.localDateToStr(fdate), mapQuotation);
            return mapQuotation;
        } catch (Exception ex) {
            log.error(ex.toString());
            throw new Exception("获取行情信息缓存失败，请稍后重试！");
        }
    }
}
